package com.vincent.controller.admin;

import com.vincent.common.enums.SessionEnum;
import com.vincent.common.enums.UserCategory;
import com.vincent.common.utils.ResultMapUtil;
import com.vincent.model.Admin;
import com.vincent.service.AdminService;
import com.vincent.vo.ForgetPwdVo;
import com.vincent.vo.LoginUserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Map;


@Controller
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private AdminService adminService;

    @RequestMapping("/login")
    public String login(HttpSession session,HttpServletRequest request){
        Object errorObj = session.getAttribute(SessionEnum.ERRORMSG.getCode());
        if(errorObj!=null){
            request.setAttribute(SessionEnum.ERRORMSG.getCode(),errorObj);
            session.removeAttribute(SessionEnum.ERRORMSG.getCode());
        }else{
            Object successObj = session.getAttribute(SessionEnum.SUCCESSMSG.getCode());
            if(successObj!=null){
                request.setAttribute(SessionEnum.SUCCESSMSG.getCode(),successObj);
                session.removeAttribute(SessionEnum.SUCCESSMSG.getCode());
            }
        }
        return "/admin/user/login";
    }

    @RequestMapping("/forgetpwd")
    public String forgetpwd(HttpSession session,HttpServletRequest request){
        Object errorObj = session.getAttribute(SessionEnum.ERRORMSG.getCode());
        if(errorObj!=null){
            request.setAttribute(SessionEnum.ERRORMSG.getCode(),errorObj);
            session.removeAttribute(SessionEnum.ERRORMSG.getCode());
        }else{
            Object successObj = session.getAttribute(SessionEnum.SUCCESSMSG.getCode());
            if(successObj!=null){
                request.setAttribute(SessionEnum.SUCCESSMSG.getCode(),successObj);
                session.removeAttribute(SessionEnum.SUCCESSMSG.getCode());
            }
        }
        return "/admin/user/forgetpwd";
    }

    @RequestMapping(value = "/user/validateLogin",method = RequestMethod.POST)
    public String validateLogin(@Valid @ModelAttribute LoginUserVo  vo,Errors errors,HttpSession session){
        Object sessionAuthcodeObj = session.getAttribute(SessionEnum.AUTHCODE.getCode());
        Map<String, Object> serviceMap = adminService.validateAdminLogin(vo, sessionAuthcodeObj);
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj !=null){
            session.setAttribute(SessionEnum.ERRORMSG.getCode(),errorObj);
            return "redirect:/admin/login.html";
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        Admin admin= (Admin) successObj;
        session.removeAttribute(SessionEnum.AUTHCODE.getCode());
        session.setAttribute(SessionEnum.ADMIN.getCode(), successObj);
        session.removeAttribute(SessionEnum.MEMBER.getCode());//移除前台会员的session
        Subject subject = SecurityUtils.getSubject();
        AuthenticationToken token=new UsernamePasswordToken(String.valueOf(admin.getId()), UserCategory.ADMIN.getCode());
        subject.login(token);
        return "redirect:/admin/main/index.html";
    }

    /**
     * 忘记密码找回邮件
     * @param vo
     * @param errors
     * @param session
     * @return
     */
    @RequestMapping(value = "/user/validateForgetPwd",method = RequestMethod.POST)
    public String validateForgetPwd(@Valid @ModelAttribute ForgetPwdVo vo,Errors errors,HttpSession session){
        Object sessionAuthcodeObj = session.getAttribute(SessionEnum.AUTHCODE.getCode());
        Map<String, Object> serviceMap = adminService.validateForgetPwdSendEmail(vo, sessionAuthcodeObj, true);
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj !=null){
            session.setAttribute(SessionEnum.ERRORMSG.getCode(),errorObj);
            return "redirect:/admin/forgetpwd.html";
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        session.setAttribute(SessionEnum.SUCCESSMSG.getCode(),successObj);
        return "redirect:/admin/login.html";
    }
    @RequestMapping("/exit")
    public String exit(){
        SecurityUtils.getSubject().logout();
        return "redirect:/admin/login.html";
    }
}
